001 /*
002 * Copyright 2005 Stephen J. McConnell.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.lang;
020
021 import java.io.File;
022 import java.io.IOException;
023 import java.net.URI;
024 import java.net.URL;
025 import java.util.List;
026 import java.util.Arrays;
027
028 /**
029 * The SystemClassLoader is a URLClassLoader that supports late binding of
030 * URLs. This class may be configured as the system classloader when loading plugins
031 * that declare system category plugin entries.
032 *
033 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
034 * @version 1.0.2
035 */
036 public final class SystemClassLoader extends StandardClassLoader
037 {
038 private final ClassLoader m_parent;
039
040 /**
041 * Creation of a new system classloader.
042 *
043 * @param parent the parent classloader
044 */
045 public SystemClassLoader( ClassLoader parent )
046 {
047 super( "system", Category.SYSTEM, new URL[0], parent );
048
049 m_parent = parent;
050
051 if( "true".equals( System.getProperty( "dpml.transit.include.tools" ) ) )
052 {
053 String jrePath = System.getProperty( "java.home" );
054 try
055 {
056 File jre = new File( jrePath );
057 File jdk = jre.getParentFile();
058 File lib = new File( jdk, "lib" );
059 File jar = new File( lib, "tools.jar" );
060 URL url = jar.toURL();
061 addURL( url );
062 }
063 catch( Throwable e )
064 {
065 final String error =
066 "Internal error while attempting to establish tools.jar in the system classloader.";
067 throw new PartError( error, e );
068 }
069 }
070 }
071
072 synchronized void addDelegates( final URI[] uris ) throws IOException
073 {
074 URL[] urls = toURLs( uris );
075 URL[] local = super.getURLs();
076 List list = Arrays.asList( local );
077 for( int i=0; i<urls.length; i++ )
078 {
079 URL url = urls[i];
080 if( !list.contains( url ) )
081 {
082 try
083 {
084 url.getContent();
085 addURL( url );
086 }
087 catch( Exception e )
088 {
089 final String error =
090 "Failed to resolve url " + url;
091 System.err.println( error );
092 }
093 }
094 }
095 }
096 }
097